// pages/sigin/index.js
const app = getApp() // 引入app
import _request from '../../utils/request.js'
import {
  getTime,
  timeFormat
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    list: [{
        day: '7',
        award: '额外奖励3意豆',
        yidou: 3,
        conSign: false,
      },
      {
        day: '14',
        award: '额外奖励8意豆',
        yidou: 8,
        conSign: false,
      },
      {
        day: '28',
        award: '额外奖励18意豆',
        yidou: 18,
        conSign: false
      },
    ],
    formatter(day) {
      return day;
    },
    weekDayArr: ['日', '一', '二', '三', '四', '五', '六'],
    time: getTime(new Date().getTime()),
    visible: false,
    numlist: [],
    msg: '',
    show: false,
    day: 0,
    active: 2,
    showitem: null,
    yidou: ''
  },

  // 关闭弹窗
  onHideshow() {
    this.setData({
      show: false
    })
  },

  // 获取当月时间
  getDay() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let dd = new Date(year, month, 0);
    //获取当月总天数
    let current_month = [];
    let weekList = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    for (let i = 1; i <= dd.getDate(); i++) {
      //数据处理
      let obj = {
        time: new Date(date.setDate(i)).getDate() + "号",
        week: weekList[new Date(date.setDate(i)).getDay()],
      };
      current_month.push(obj);
    }
    console.log(current_month);
  },

  // 领取奖励
  getAward(e) {
    const {
      item
    } = e.currentTarget.dataset
    _request.get('/userSignin/otherSign', {
      userId: wx.getStorageSync('userid'),
      day: item.day
    }).then((res) => {
      this.setData({
        active: 2,
        show: true,
        showitem: item
      })
      this.getfindUserSignin()
      this.getisSign()
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 签到
  getsigin() {
    _request.get('/userSignin/signin', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.getfindSignNumber()
      this.setData({
        active: 1,
        show: true,
        yidou: res.data.data
      })
      this.getfindUserSignin()
      this.getisSign()
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  getfindUserSignin() {
    let vm = this
    vm.data.numlist = []
    _request.get('/userSignin/findUserSignin', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      if (res.data.code == 200) {
        // 获取当天是否签到
        res.data.data.map(item => {
          let a = timeFormat(item.updatetime, 3)
          if (item.signTime == vm.data.time) {
            vm.setData({
              visible: true,
            })
          }
          vm.setData({
            numlist: [...vm.data.numlist, a],
          })
        })
      }
      // 已签到的时间
      vm.setData({
        formatter(day) {
          const date = day.date.getDate();
          if (vm.data.numlist.indexOf(date) > -1) {
            day.bottomInfo = '●';
          }
          return day;
        }
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 连续签到奖励是否可领取
  getisSign() {
    _request.get('/userSignin/isSign', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      let data = res.data.data
      this.setData({
        list: this.data.list.map(item => {
          if (item.day == '7') {
            item.conSign = data.conSign7
          } else if (item.day == '14') {
            item.conSign = data.conSign14
          } else if (item.day == '28') {
            item.conSign = data.conSign28
          }
          return item
        })
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  // 获取签到天数
  getfindSignNumber() {
    _request.get('/userSignin/findSignNumber', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        msg: res.data.message,
        day: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 查询签到记录
    this.getfindUserSignin()
    // 获取签到天数
    this.getfindSignNumber()

    // 连续签到奖励是否可领取
    this.getisSign()

    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})